package cn.zmwh.manage.admin.modules.ext.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.constants.Constants;
import cn.zmwh.common.mybatis.page.PageRequest;
import cn.zmwh.manage.admin.modules.ext.pojo.dto.SysDictDto;
import cn.zmwh.manage.admin.modules.ext.pojo.entity.SysDict;
import cn.zmwh.manage.admin.modules.ext.pojo.entity.SysDictItem;
import cn.zmwh.manage.admin.modules.ext.service.SysDictItemService;
import cn.zmwh.manage.admin.modules.ext.service.SysDictService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author dmzmwh
 * @since 2025-03-12
 */
@Slf4j
@Api(tags = "字典表")
@RestController
@AllArgsConstructor
@RequestMapping("/api/ext/dict")
public class SysDictController {


    private final SysDictService sysDictService;

    private final SysDictItemService sysDictItemService;


    @PostMapping("/save")
    @ApiOperation(value = "保存字典")
    public Result save(SysDictDto sysDictDto,
                       @ApiIgnore @RequestHeader(Constants.UID) Long uid) {
        List<SysDictItem> list = sysDictDto.getList();
        for (SysDictItem sysDictItem : list) {
            sysDictItem.setDictType(sysDictDto.getDictType());
        }
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(sysDictDto, sysDict);
        sysDict.setUpdateBy(uid);
        sysDict.setUpdateTime(new Date());

        List<Long> ids = sysDictDto.getIds();
        if (CollUtil.isNotEmpty(ids)) {
            sysDictItemService.removeBatchByIds(ids);
        }
        sysDictService.saveOrUpdate(sysDict);
        sysDictItemService.saveBatch(list);
        return Result.success();
    }

    /**
     * 查询字典信息
     *
     * @param dictType 查询信息
     * @return 字典信息
     */
    @GetMapping("/type")
    @ApiOperation(value = "查询字典信息")
    public Result getDetails(String dictType) {
        SysDictDto sysDictDto = new SysDictDto();
        SysDict one = sysDictService.getOne(Wrappers.<SysDict>query().lambda().eq(SysDict::getDictType, dictType));
        if (one != null) {
            BeanUtil.copyProperties(one, sysDictDto);
        }
        List<SysDictItem> list = sysDictItemService.list(Wrappers.<SysDictItem>query()
                .lambda().eq(SysDictItem::getDictType, dictType));
        sysDictDto.setList(list);
        return Result.ok(list);
    }

    /**
     * 通过字典类型查找字典
     *
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/qry")
    @ApiOperation(value = "查询字典")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "size", value = "每页数据"),
                    @ApiImplicitParam(name = "current", value = "页码"),
                    @ApiImplicitParam(name = "dictType", value = "描述或者类型"),
            }
    )
    public Result<IPage<SysDict>> list(String dictType, PageRequest<SysDict> request) {
        LambdaQueryWrapper<SysDict> and = Wrappers.<SysDict>query().lambda()
                .and(StrUtil.isNotBlank(dictType), wrapper -> wrapper.like(SysDict::getDescription, dictType)
                        .or()
                        .like(SysDict::getDescription, dictType));
        Page<SysDict> pageR = sysDictService.page(request, and);
        return Result.ok(pageR);
    }


}

